﻿namespace ES.Admin
{
    using System.Linq;
    using System.Windows.Forms;

    public partial class FrmYeniUrun : Form
    {
        public int? Id { get; set; }
        private Product _product = new Product();
        public FrmYeniUrun()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            _product.Title = textBox1.Text;
            _product.Price = double.Parse(textBox2.Text);
            _product.Kdv = double.Parse(textBox3.Text);
            _product.Model = textBox4.Text;
            _product.Attributes = textBox5.Text;
            _product.Description = textBox6.Text;
            _product.ImageUrl = textBox7.Text;
            _product.Seo = textBox8.Text;
            _product.Category = (Category)comboBox1.SelectedItem;
            if (!Id.HasValue) { Program.Db.Products.InsertOnSubmit(_product); }
            Program.Db.SubmitChanges();
            Close();
        }

        private void FrmYeniUrun_Load(object sender, System.EventArgs e)
        {
            comboBox1.Items.AddRange(Program.Db.Categories.ToArray());
            comboBox1.DisplayMember = "Text";
            comboBox1.ValueMember = "Id";
            if (Id.HasValue)
            {
                _product = Program.Db.Products.SingleOrDefault(d => d.Id == Id);

                textBox1.Text = _product.Title;
                textBox2.Text = _product.Price.ToString();
                textBox3.Text = _product.Kdv.ToString();
                textBox4.Text = _product.Model;
                textBox5.Text = _product.Attributes;
                textBox6.Text = _product.Description;
                textBox7.Text = _product.ImageUrl;
                textBox8.Text = _product.Seo;
                comboBox1.SelectedItem = _product.Category;
            }
        }
    }
}
